package com.example.fpe_vote.db1.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.example.fpe_vote.common.Result;
import com.example.fpe_vote.common.Search;
import com.example.fpe_vote.db1.entity.PrizeInfo;
import com.example.fpe_vote.db1.entity.VoteInfo;
import com.example.fpe_vote.db1.service.IPrizeInfoService;
import com.example.fpe_vote.utils.VoUtilsTool;
import com.example.fpe_vote.vo.request.*;
import com.example.fpe_vote.vo.response.ResponseGetPrizeInfoVo;
import com.example.fpe_vote.vo.response.ResponseListPrizeHtVo;
import com.example.fpe_vote.vo.response.ResponseListPrizeInfoVo;
import com.example.fpe_vote.vo.response.ResponseSelectPrizeVo;
import javafx.concurrent.Worker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2022-12-01
 */
@RestController
@RequestMapping("/prize-info")
public class PrizeInfoController {
    @Autowired
    IPrizeInfoService prizeInfoService;

    @PostMapping("/getAll")
    public Result getAll(){
        List<PrizeInfo> prizeInfoList=prizeInfoService.list();
        return Result.getSuccess().setData(prizeInfoList);
    }

    @GetMapping("/getPrizeInfo/{prizeNum}")
    public Result getPrizeInfo(@PathVariable String prizeNum){
        PrizeInfo prizeInfo=prizeInfoService.queryByNum(prizeNum);
        ResponseGetPrizeInfoVo vo=new ResponseGetPrizeInfoVo();
        BeanUtils.copyProperties(prizeInfo,vo);
        return Result.getSuccess().setData(vo);
    }

    @PostMapping("/editPrize")
    public Result editPrize(@RequestBody RequestEditPrizeVo vo){
        PrizeInfo prizeInfo=prizeInfoService.queryByNum(vo.getPrizeNum());
        prizeInfo.setPrizeName(vo.getPrizeName());
        prizeInfo.setAllowAmounts(vo.getAllowAmounts());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime endTime= LocalDate.parse(vo.getEndTime(),formatter).atStartOfDay();
        prizeInfo.setEndTime(endTime);
        if(prizeInfoService.updateById(prizeInfo)){
            return Result.getSuccess().setMsg("保存成功！");
        }else
            return Result.getFailure().setMsg("保存失败");
    }

    //分页查询奖项
    @PostMapping("/listPrize")
    public Result listPrize(@RequestBody RequestListPrizeVo vo){
        IPage<ResponseListPrizeInfoVo> iPage=prizeInfoService.listPrize(vo);
        return Result.getSuccess().setData(iPage);
    }

    //分页查询后台
    @PostMapping("/listPrizeHt")
    public Result listPrizeHt(@RequestBody RequestListPrizeHtVo vo){
        IPage<ResponseListPrizeHtVo> iPage=prizeInfoService.listPrizeHt(vo);
        return Result.getSuccess().setData(iPage);
    }

    //搜索奖项
    @PostMapping("/searchPrize")
    public Result searchPrize(@RequestBody RequestSearchPrizeInfoVo vo){
        List<ResponseListPrizeInfoVo> voList=prizeInfoService.searchPrize(vo);
        return Result.getSuccess().setData(voList);
    }

    //发布新奖项
    @PostMapping("/addPrize")
    public Result addPrize(@RequestBody RequestAddPrizeVo vo){
        if(VoUtilsTool.checkObjFieldIsNull(vo)){
            return Result.getFailure().setMsg("请填写完整信息!");
        }
        PrizeInfo prizeInfo=new PrizeInfo();
        BeanUtils.copyProperties(vo,prizeInfo);
        prizeInfo.setPrizeNum(IdWorker.getIdStr());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime endTime= LocalDate.parse(vo.getEndTime(),formatter).atStartOfDay();
        prizeInfo.setEndTime(endTime);
        if(prizeInfoService.save(prizeInfo)){
            return Result.getSuccess().setMsg("发布成功");
        }else
            return Result.getFailure().setMsg("发布失败");
    }

    //下拉框带出奖项
    @PostMapping("/selectPrize")
    public Result selectPrize(){
        List<ResponseSelectPrizeVo> list=new ArrayList<>();
        List<PrizeInfo> prizeInfos=prizeInfoService.list();
        for(PrizeInfo prizeInfo:prizeInfos){
            ResponseSelectPrizeVo vo=new ResponseSelectPrizeVo();
            BeanUtils.copyProperties(prizeInfo,vo);
            list.add(vo);
        }
        return Result.getSuccess().setData(list);
    }

    @GetMapping("/deletePrize/{prizeNum}")
    public Result deletePrize(@PathVariable String prizeNum){
        PrizeInfo prizeInfo=prizeInfoService.queryByNum(prizeNum);
        if(prizeInfoService.removeById(prizeInfo.getId())){
            return Result.getSuccess().setMsg("删除成功！");
        }else
            return Result.getFailure().setMsg("删除失败...");
    }

}
